package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CommentMapper;
import com.itheima.pojo.Comment;
import com.itheima.service.CommentService;
import com.itheima.utils.UuidUtil;
import com.itheima.vo.CommentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;


    /**
     * 添加评论
     *
     * @param commentVo
     */
    @Override
    @Transactional
    public void add(CommentVo commentVo) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentVo, comment);
        comment.setCreateTime(new Date());
        if (commentVo.getUserUid() != null && "".equals(commentVo.getUserUid())) {
            comment.setUserUid(commentVo.getUserUid());
        }
        if (comment.getUid() == null) {
            comment.setUid(UuidUtil.getUuid());
        }
        //用户Uid
        //该评论评论的uid  1
        //该评论回复的用户id
        if (commentVo.getToUserUid()!=null&&!"".equals(commentVo.getToUserUid())){
            comment.setToUserUid(commentVo.getToUserUid());
        }
        if (commentVo.getToUid()!=null&&!"".equals(commentVo.getToUid())){
            commentVo.setToUid(commentVo.getToUid());
        }
        comment.setUpdateTime(new Date());

        comment.setSource("ABOUT");
        //TODO 登录人UserUid暂时没有写
        comment.setTYPE((byte) 0);
        commentMapper.insertSelective(comment);
    }

    @Override
    public PageInfo<Comment> list(CommentVo commentVo) {
        if (commentVo.getCurrentPage()!=null&&commentVo.getPageSize()!=null){
            PageHelper.startPage(commentVo.getCurrentPage(),commentVo.getPageSize());
        }
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if (commentVo.getUserName()!=null&&!"".equals(commentVo.getUserName())){
            criteria.andEqualTo("userName",commentVo.getUserName());
        }
        if (commentVo.getKeyword()!=null&&!"".equals(commentVo.getKeyword())){
            criteria.orLike("content",commentVo.getKeyword());
        }
        List<Comment> comments = commentMapper.selectByExample(example);

        return new PageInfo<>(comments);
    }

    /**
     * 删除
     * @param commentVo
     */
    @Override
    @Transactional
    public void delete(CommentVo commentVo) {
        Comment comment = new Comment();
       // 用工具类复制属性给comment实体类
        BeanUtils.copyProperties(commentVo,comment);
        //uid不能为空 useruid不能为空
        int i = commentMapper.deleteByPrimaryKey(comment);
        if (i==0){
            throw new RuntimeException("评论不存在");
        }
    }

    /**
     * 批量删除
     * @param commentVoList
     */
    @Override
    @Transactional
    public void deleteBatch(List<CommentVo> commentVoList) {
        //判断前端传来的CommentVo集合是否为空
        if (commentVoList==null){
            //为空就直接返回
          throw new RuntimeException("评论不存在");
        }
        //遍历集合   用工具类复制属性给comment实体类
        for (CommentVo commentVo : commentVoList) {
            Comment comment = new Comment();
            BeanUtils.copyProperties(commentVo,comment);
            //通用mapper删除评论
            int i = commentMapper.deleteByPrimaryKey(comment);
            if (i==0){
                throw new RuntimeException("评论不存在");
            }
        }
    }

    @Override
    @Transactional
    public void edit(CommentVo commentVo) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentVo,comment);
        int i = commentMapper.updateByPrimaryKeySelective(comment);
        if (i==0) {
            throw new RuntimeException("没有这个评论");
        }
    }
}
